﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace slwhTools.ExcelHelper
{
    internal class SDomain
    {

        public static PropertyInfo[] GetPropertiesOfType<T>()
        {
            // 获取类型 T 的所有公共属性
            return typeof(T).GetProperties();
        }

        /// <summary>
        /// 反射结构体的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<object, PropertyInfo> SRefObject<T>(KeyEnum keyStr = KeyEnum.Column)
        {
            var mappings = new Dictionary<object, PropertyInfo>();
            PropertyInfo[] properties = typeof(T).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                switch (keyStr)
                {
                    case KeyEnum.Column:
                        {
                            ColumnAttribute columnAttribute = property.GetCustomAttribute<ColumnAttribute>();
                            if (columnAttribute != null)
                            {
                                mappings[columnAttribute.Name] = property;
                            }
                        }
                        break;
                    case KeyEnum.Name: { mappings[property.Name] = property; } break;
                    case KeyEnum.PropertyType: { mappings[property.PropertyType] = property; } break;
                    default: mappings[property.Name] = property; break;
                }
            }

            return mappings;
        }

        /// <summary>
        /// 反射结构体的属性(有实例的)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public Dictionary<object, PropertyInfo> SRefObject(object obj, KeyEnum keyStr = KeyEnum.Column)
        {
            if (obj == null) throw new ArgumentNullException(nameof(obj));

            var mappings = new Dictionary<object, PropertyInfo>();

            foreach (var property in obj.GetType().GetProperties())
            {
                switch (keyStr)
                {
                    case KeyEnum.Column:
                        {
                            ColumnAttribute columnAttribute = property.GetCustomAttribute<ColumnAttribute>();
                            if (columnAttribute != null)
                            {
                                mappings[columnAttribute.Name] = property;
                            }
                        }
                        break;
                    case KeyEnum.Name: { mappings[property.Name] = property; } break;
                    case KeyEnum.PropertyType: { mappings[property.PropertyType] = property; } break;
                    default: mappings[property.Name] = property; break;
                }
            }
            return mappings;
        }


    }

   
}
